<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge"> -->
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
    <title>react</title>
</head>

<body>
    <!-- 准备好一个容器 -->
    <div class="test"></div>
    <div class="test2"></div>

    <!-- js按顺序引入 -->
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>

    <!-- 引入react-dom,用于操作dom -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>

    <!-- 引入babel,用于将jsx转为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>

    <!-- 引入prop-types,用于对组件标签进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>

    <script type="text/babel">
        // 1.创建类式组件 组件名首字母必须大写
        class Life extends React.Component{    
            state = {
                opacity:1
            }
            // 组件挂载完成后触发的生命周期
            componentDidMount(){
                // 200ms透明度减0.1
                this.timer = setInterval(()=>{
                let opacity = this.state.opacity
                opacity -= 0.1
                if(opacity <= 0) opacity = 1
                this.setState({opacity})
                },200)

            }

            // 组件将要卸载执行的生命周期
            componentWillUnmount(){
                // 清除定时器
                clearInterval(this.timer)
                }

            render(){
                return (
                    <div>
                        <div style={{opacity:this.state.opacity}} >啊6666666</div>
                        <button onClick={this.handelLife}>点击销毁生命周期</button>
                        </div>
                    )
            }
            handelLife = ()=>{
                // 清除定时器
                clearInterval(this.timer)
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.querySelector(".test"))
            }
        }
 
    
      // 2.渲染虚拟Dom 
      ReactDOM.render(<Life/>, document.querySelector(".test"));
    </script>
</body>
<style>

</style>

</html>